package com.tadpole.music.view.activity.question;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.luck.picture.lib.permissions.RxPermissions;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.question.SimulationPracticeAdapter;
import com.tadpole.music.bean.question.QuestionClassBean;
import com.tadpole.music.bean.question.TrainingQuestionBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.question.QuestionClassIView;
import com.tadpole.music.iView.question.TrainingQuestionIView;
import com.tadpole.music.presenter.question.QuestionClassPresenter;
import com.tadpole.music.presenter.question.TrainingQuestionPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.me.MemberCenterActivity;

import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * 专项练习
 * Created By 孙岩
 * on 2020/9/24
 */
public class SpecialExercisesActivity extends BaseActivity implements QuestionClassIView , TrainingQuestionIView {
    //回退
    private View view_back_icon;
    //标题
    private TextView actionbar_title;
    //名称
    private TextView tvName;
    //recyclerView
    private RecyclerView recyclerView;
    //adapter
    private SimulationPracticeAdapter simulationPracticeAdapter;
    //学院id
    private String college_id;
    //等级id
    private String rank_id;
    //学院名称
    private String name;
    //获取分类
    private QuestionClassPresenter questionClassPresenter;
    //获取题目
    private TrainingQuestionPresenter trainingQuestionPresenter;
    //题目id
    private String question_class_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_exercises);

        college_id = getIntent().getStringExtra("college_id");
        rank_id = getIntent().getStringExtra("rank_id");
        name = getIntent().getStringExtra("name");
        initViews();
        initListeners();

        questionClassPresenter = new QuestionClassPresenter();
        questionClassPresenter.attachView(this);
        questionClassPresenter.getQuestion_class(college_id, rank_id);

        //获取题目
        trainingQuestionPresenter = new TrainingQuestionPresenter();
        trainingQuestionPresenter.attachView(this);

    }

    //初始化控件
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        actionbar_title = findViewById(R.id.actionbar_title);
        actionbar_title.setText("专项练习");
        tvName = findViewById(R.id.tvName);
        tvName.setText(name);
        recyclerView = findViewById(R.id.recyclerView);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                SpecialExercisesActivity.this.finish();
            }
        });
    }

    @Override
    public void showQuestionClass(final List<QuestionClassBean.DataBean> list) {
        GridLayoutManager layoutManager = new GridLayoutManager(SpecialExercisesActivity.this, 4);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        simulationPracticeAdapter = new SimulationPracticeAdapter(SpecialExercisesActivity.this, list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(simulationPracticeAdapter);
        recyclerView.setNestedScrollingEnabled(false);

        simulationPracticeAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if(Mutils.isNetworkAvailable()){
                    question_class_id = String.valueOf(list.get(position).getId());

                    andPermission(String.valueOf(list.get(position).getId()));
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    //权限
    @SuppressLint("CheckResult")
    private void andPermission(final String question_class_id) {
        RxPermissions rxPermission = new RxPermissions(SpecialExercisesActivity.this);
        rxPermission
                .request(
                        Manifest.permission.MODIFY_AUDIO_SETTINGS,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                ).subscribe(new Consumer<Boolean>() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                //请求权限成功
                if (aBoolean) {
                    trainingQuestionPresenter.getTrainingQuestion(1, college_id, rank_id, question_class_id, 0);
                } else {//失败
                    String msg = "请点击【去授权】打开相应权限！";
                    showAlert(msg);
                }
            }
        });
    }

    //授权
    public void showAlert(String msg) {
        new AlertDialog.Builder(SpecialExercisesActivity.this).setMessage(msg).
                setPositiveButton("去授权",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", SpecialExercisesActivity.this.getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setCancelable(true).show();
    }

    @Override
    public void show401() {
        Intent intent = new Intent(SpecialExercisesActivity.this, LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(SpecialExercisesActivity.this).putString(Constant.TOKEN,"");
    }

    @Override
    public void goToPay() {
        Intent intent = new Intent(SpecialExercisesActivity.this, MemberCenterActivity.class);
        intent.putExtra("flag","1");
        intent.putExtra("college_id",college_id);
        intent.putExtra("rank_id",rank_id);
        startActivity(intent);
    }

    @Override
    public void showQuestion(TrainingQuestionBean.DataBeanX bean) {
        Intent intent = new Intent(SpecialExercisesActivity.this, SimulationExerciseActivity.class);
        intent.putExtra("college_id", college_id);
        intent.putExtra("question_rank_id", rank_id);
        intent.putExtra("question_class_id", question_class_id);
        startActivity(intent);
    }

    @Override
    public void go401() {
        Intent intent = new Intent(SpecialExercisesActivity.this, LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(SpecialExercisesActivity.this).putString(Constant.TOKEN,"");
    }
}
